package com.zwxbc.funplay.servlet;

import com.google.gson.Gson;
import com.zwxbc.funplay.entity.GameCategory;
import com.zwxbc.funplay.service.GameCategoryService;
import com.zwxbc.funplay.service.impl.GameCategoryServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/gameCategory")
public class GameCategoryServlet extends HttpServlet {
	GameCategoryService gameCategoryService = new GameCategoryServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");

		String action = request.getParameter("action");

		if ("getAll".equals(action)) {
			// 获取所有游戏分类数据
			handleGetList(request, response);
		} else if ("getById".equals(action)) {
			// 获取单个游戏分类数据
			handleGetById(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("未知的操作");
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String action = request.getParameter("action");

		System.out.println(action);
		if ("add".equals(action)) {
			// 添加游戏分类
			handleAdd(request, response);
		} else if ("update".equals(action)) {
			// 更新游戏分类
			handleUpdate(request, response);
		} else if ("delete".equals(action)) {
			// 删除游戏分类
			handleDelete(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("Unknown action");
		}
	}

	// 添加新游戏分类
	private void handleAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String gameCategoryName = request.getParameter("gameCategoryName");

		// 创建一个 GameCategory 对象
		GameCategory newGameCategory = new GameCategory();
		newGameCategory.setCategoryName(gameCategoryName);

		// 调用服务方法添加新游戏分类
		boolean success = gameCategoryService.addGameCategory(newGameCategory);

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 更新游戏分类
	private void handleUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String gameCategoryIdParam = request.getParameter("gameCategoryId");
		Long gameCategoryId = Long.parseLong(gameCategoryIdParam);
		String gameCategoryName = request.getParameter("gameCategoryName");

		GameCategory updatedGameCategory = new GameCategory();
		updatedGameCategory.setCategoryId(gameCategoryId);
		updatedGameCategory.setCategoryName(gameCategoryName);
		System.out.println(updatedGameCategory);
		// 调用服务方法更新游戏分类
		boolean success = gameCategoryService.updateGameCategory(updatedGameCategory);
		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 删除游戏分类
	private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String gameCategoryId = request.getParameter("gameCategoryId");

		// 调用服务方法删除游戏分类
		boolean success = gameCategoryService.deleteGameCategory(Long.parseLong(gameCategoryId));

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 根据ID查询游戏分类
	private void handleGetById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String gameCategoryIdParam = request.getParameter("gameCategoryId");

		Long gameCategoryId = Long.parseLong(gameCategoryIdParam);

		// 调用方法根据ID获取游戏分类
		GameCategory gameCategory = gameCategoryService.getGameCategoryById(gameCategoryId);

		// 将 GameCategory 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(gameCategory);
		sendJsonResponse(response, jsonData);
	}


	private void handleGetList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<GameCategory> gameCategoryList = gameCategoryService.getGameCategoryList();
		// 将 GameCategory 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(gameCategoryList);
		sendJsonResponse(response, jsonData);
	}

	private void sendJsonResponse(HttpServletResponse response, String jsonData) throws IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonData);
		out.flush();
	}
}
